﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNAVE3D
{
    class Plano
    {
        public VertexPositionTexture[] verts;
        Vector3 position;
        public Texture2D Texture; Texture2D Texture2;
        public  Matrix myWorld = Matrix.Identity;
        Effect effect; bool shader;
        bool sortTextura; float time;
        public Plano(Vector3 position, Texture2D texture, Texture2D texture2, Vector3 tamanho, Effect effect, bool shader)
        {
            // nao crie um plano inclinado, crie e depois rotacione
            myWorld.Translation = position; 
            this.position = position;
            this.shader = shader;
            Texture = texture;
            Texture2 = texture2;
            this.effect = effect;
            if (tamanho.X > 0 && tamanho.Z > 0)
            {
                verts = new VertexPositionTexture[4];
                verts[0] = new VertexPositionTexture(new Vector3(0 + tamanho.X, 0 - tamanho.Y, 0 + tamanho.Z), new Vector2(1, 1));
                verts[1] = new VertexPositionTexture(new Vector3(0 + tamanho.X, 0 - tamanho.Y, 0 - tamanho.Z), new Vector2(0, 1));
                verts[2] = new VertexPositionTexture(new Vector3(0 - tamanho.X, 0 + tamanho.Y, 0 + tamanho.Z), new Vector2(1, 0));
                verts[3] = new VertexPositionTexture(new Vector3(0 - tamanho.X, 0 + tamanho.Y, 0 - tamanho.Z), new Vector2(0, 0));
            }
            if (tamanho.Y > 0&& tamanho.Z > 0)
            {
                verts = new VertexPositionTexture[4];
                verts[0] = new VertexPositionTexture(new Vector3(0 + tamanho.X, 0 - tamanho.Y, 0 + tamanho.Z), new Vector2(1, 1));
                verts[1] = new VertexPositionTexture(new Vector3(0 - tamanho.X, 0 - tamanho.Y, 0 - tamanho.Z), new Vector2(0, 1));
                verts[2] = new VertexPositionTexture(new Vector3(0 + tamanho.X, 0 + tamanho.Y, 0 + tamanho.Z), new Vector2(1, 0));
                verts[3] = new VertexPositionTexture(new Vector3(0 - tamanho.X, 0 + tamanho.Y, 0 - tamanho.Z), new Vector2(0, 0));
            }
            if (tamanho.Y > 0 && tamanho.X > 0)
            {
                verts = new VertexPositionTexture[4];
                verts[0] = new VertexPositionTexture(new Vector3(0 - tamanho.X, 0 - tamanho.Y, 0 + tamanho.Z), new Vector2(1, 1));
                verts[1] = new VertexPositionTexture(new Vector3(0 + tamanho.X, 0 - tamanho.Y, 0 - tamanho.Z), new Vector2(0, 1));
                verts[2] = new VertexPositionTexture(new Vector3(0 - tamanho.X, 0 + tamanho.Y, 0 + tamanho.Z), new Vector2(1, 0));
                verts[3] = new VertexPositionTexture(new Vector3(0 + tamanho.X, 0 + tamanho.Y, 0 - tamanho.Z), new Vector2(0, 0));
            }
        }
        public void Update(GameTime gameTime)
        {
            myWorld.Translation = position;
            if (shader)
            {
                time += gameTime.ElapsedGameTime.Milliseconds * 0.00005f;
                if (time >= 1.0f)
                {
                    time = 0;
                    sortTextura = !sortTextura;
                }
            }
        }
        public void Draw(Matrix view, Matrix projection, GraphicsDevice g)
        {

            effect.CurrentTechnique = effect.Techniques["Technique1"];
            effect.Parameters["View"].SetValue(view);
            effect.Parameters["Projection"].SetValue(projection);
            effect.Parameters["World"].SetValue(myWorld);
            effect.Parameters["t"].SetValue(time);
            if (!shader)
            {
                effect.Parameters["texOrigin"].SetValue(this.Texture);
            }
            else if (shader)
            {
                if (this.sortTextura)
                {
                    effect.Parameters["texOrigin"].SetValue(this.Texture2);
                    effect.Parameters["texDestiny"].SetValue(this.Texture);
                }
                else
                {
                    effect.Parameters["texOrigin"].SetValue(this.Texture);
                    effect.Parameters["texDestiny"].SetValue(this.Texture2);
                }
            }
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                g.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 0, 2);                
            }
                        
        }

    }
}
